package com.sixth.move.move_shopping_mall.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sixth.move.move_shopping_mall.domain.Employee;
import com.sixth.move.move_shopping_mall.domain.EmployeeExample;
import com.sixth.move.move_shopping_mall.domain.SearchEmployeeRequest;
import com.sixth.move.move_shopping_mall.domain.SearchEmployeeResponse;
import com.sixth.move.move_shopping_mall.mapper.EmployeeMapper;
import com.sixth.move.move_shopping_mall.service.EmployeeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("DBEmployeeService")
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public SearchEmployeeResponse listEmployee(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> employees = employeeMapper.selectByExample(new EmployeeExample());
        return new SearchEmployeeResponse(((Page)employees).getTotal(), employees);
    }

    @Override
    public int deleteEmployeeByPrimaryKey(String id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Employee findEmployeeByPrimaryKey(String id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> searchEmployees(SearchEmployeeRequest request) {
        EmployeeExample ex = new EmployeeExample();
//        ex.createCriteria().andEmployeeIdEqualTo(request.getId());
//        ex.or().andLastNameLike("%" + request.getName() + "%");
        return employeeMapper.selectByExample(ex);
    }

    @Override
    public int saveEmployee(Employee employee) {
        if(StringUtils.isNotBlank(employee.getEmployeeId())) {
            return employeeMapper.updateByPrimaryKey(employee);
        } else {
            return employeeMapper.insert(employee);
        }
    }
}
